package com.showsu.editor.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.showsu.editor.dao.TblRemoteInterfaceSwitchRecordMapper;
import com.showsu.editor.entity.doo.RemoteInterfaceSwitchRecord;
import com.showsu.editor.entity.po.TblRemoteInterfaceSwitchRecord;
import com.showsu.editor.enums.RowFlagEnum;
import com.showsu.editor.service.IRemoteInterfaceSwitchRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: William
 * @Description:
 * @Date: Created in 3:16 下午 2024/3/8
 * @Modified By:
 */
@Service
@Slf4j
public class RemoteInterfaceSwitchRecordServiceImpl extends ServiceImpl<TblRemoteInterfaceSwitchRecordMapper, TblRemoteInterfaceSwitchRecord> implements IRemoteInterfaceSwitchRecordService {

    @Autowired
    private TblRemoteInterfaceSwitchRecordMapper tblRemoteInterfaceSwitchRecordMapper;


    @Override
    public RemoteInterfaceSwitchRecord queryLastSwitchRecordByInterfaceName(String interfaceName) {
        if (StringUtils.isBlank(interfaceName)) {
            return null;
        }
        TblRemoteInterfaceSwitchRecord tblRemoteInterfaceSwitchRecord = tblRemoteInterfaceSwitchRecordMapper.selectOne(new QueryWrapper<TblRemoteInterfaceSwitchRecord>().lambda()
                .eq(TblRemoteInterfaceSwitchRecord::getInterfaceName, interfaceName)
                .eq(TblRemoteInterfaceSwitchRecord::getFlag, RowFlagEnum.UnDelete.getCode())
                .orderByDesc(TblRemoteInterfaceSwitchRecord::getId)
                .last("limit 1"));
        return tblRemoteInterfaceSwitchRecord == null ? null : BeanUtil.copyProperties(tblRemoteInterfaceSwitchRecord, RemoteInterfaceSwitchRecord.class);
    }

    @Override
    public int add(String interfaceName, Long interfaceId, String params) {
        if (StringUtils.isBlank(interfaceName) || interfaceId == null) {
            return 0;
        }
        TblRemoteInterfaceSwitchRecord record = new TblRemoteInterfaceSwitchRecord();
        record.setInterfaceName(interfaceName);
        record.setInterfaceId(interfaceId);
        record.setQueryParams(params);
        return tblRemoteInterfaceSwitchRecordMapper.insert(record);
    }

    @Override
    public RemoteInterfaceSwitchRecord queryLastSwitchRecordByInterfaceId(Long interfaceId) {
        if (interfaceId == null) {
            return null;
        }
        TblRemoteInterfaceSwitchRecord tblRemoteInterfaceSwitchRecord =  tblRemoteInterfaceSwitchRecordMapper.selectOne(new QueryWrapper<TblRemoteInterfaceSwitchRecord>().lambda()
                .eq(TblRemoteInterfaceSwitchRecord::getInterfaceId, interfaceId)
                .eq(TblRemoteInterfaceSwitchRecord::getFlag, RowFlagEnum.UnDelete.getCode())
                .orderByDesc(TblRemoteInterfaceSwitchRecord::getId)
                .last("limit 1"));
        return tblRemoteInterfaceSwitchRecord == null ? null : BeanUtil.copyProperties(tblRemoteInterfaceSwitchRecord, RemoteInterfaceSwitchRecord.class);
    }

    @Override
    public List<RemoteInterfaceSwitchRecord> queryLastSwitchRecordInRecentTimesGroupByInterfaceId(Date time) {
        if (time == null || time.getTime() > System.currentTimeMillis()) {
            return null;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("createTime", time);
        List<TblRemoteInterfaceSwitchRecord> tblRemoteInterfaceSwitchRecordList = tblRemoteInterfaceSwitchRecordMapper.queryLastSwitchRecordInRecentTimesGroupByInterfaceId(params);
        return CollectionUtils.isEmpty(tblRemoteInterfaceSwitchRecordList) ? null
                : tblRemoteInterfaceSwitchRecordList.stream().map(
                tblRemoteInterfaceSwitchRecord -> { return BeanUtil.copyProperties(tblRemoteInterfaceSwitchRecord, RemoteInterfaceSwitchRecord.class); }
        ).collect(Collectors.toList());
    }
}
